package com.zmn.oms.common.constant;

import com.zmn.common.utils.string.StringUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 类描述：工单取消常量
 *
 * @author liuying
 * @date 2022/03/09 17:43
 */
public class ConfReplyConsts {

    /**
     * 功能描述 :针对$1$2$这种分割的字符串 按照传入的kvMap拼接他们的中文释意
     *
     * @param idStr
     * @param kvMap
     * @return java.lang.String
     * @author linfeng
     * @since 2020/04/16
     */
    public static String getUnionNameBykvMapIdStr(String idStr, Map<Integer, String> kvMap) {
        if (StringUtil.isNotBlank(idStr)) {
            idStr = trimBothEndsChars(idStr, "\\$");
            List<Integer> plats = Arrays.stream(idStr.split("\\$")).map(e -> Integer.parseInt(e)).collect(Collectors.toList());
            StringBuilder stringBulider = new StringBuilder();
            plats.forEach(e -> stringBulider.append(kvMap.get(e)).append("、"));
            if (stringBulider.length() < 1) {
                return "";
            }
            return stringBulider.deleteCharAt(stringBulider.length() - 1).toString();
        }
        return "";
    }

    private static String trimBothEndsChars(String srcStr, String splitter) {
        String regex = "^" + splitter + "*|" + splitter + "*$";
        return srcStr.replaceAll(regex, "");
    }
}
